﻿using System.Numerics;
using WaveSim.Core.Methods.ODE;
using WaveSim.Core.Equations;

namespace WaveSim.Core.Research
{
    public class FieldCalculator
    {
        private IMethodODE method;
        private IEquation equation;

        public FieldCalculator(IEquation equation, IMethodODE method)
        {
            this.equation = equation;
            this.method = method;
        }

        public Complex[] Get(Complex[] startValues, double x0, double x1, int n)
        {
            Complex[] y = (Complex[])startValues.Clone();
            double h = (x1 - x0) / n;
            for (int i = 1; i <= n; i++)
            {
                double x = x0 + i * h;
                method.Solve(x, h, y, equation);
            }
            return y;
        }

        public Complex[,] GetFull(Complex[] startValues, double x0, double x1, int n)
        {
            Complex[] y = (Complex[])startValues.Clone();
            double h = (x1 - x0) / n;

            Complex[,] data = new Complex[n + 1, y.Length];
            Copy(y, data, 0);
            for (int i = 1; i <= n; i++)
            {
                double x = x0 + i * h;
                method.Solve(x, h, y, equation);
                Copy(y, data, i);
            }
            return data;
        }

        #region Utility
        private void Copy(Complex[] source, Complex[,] destination, int index)
        {
            for (int i = 0; i < source.Length; i++)
            {
                destination[index, i] = source[i];
            }
        }
        #endregion
    }
}
